package yxy.game.pm2.module.battle;

import org.apache.commons.lang3.StringUtils;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.NiuBee;
import yxy.cherry.battle.Pokemon;
import yxy.cherry.battle.Team;
import yxy.cherry.battle.module.*;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.fetterdata;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.data.bean.weapondata;
import yxy.game.pm2.module.pickedtroops.PickedTroops;
import yxy.game.pm2.module.pickedtroops.PickedTroopsModule;

import java.util.*;
import java.util.stream.Collectors;

public class TeamModule {

    // 队伍数据
    private FighterModule fighter_FU;
    private FighterModule fighter_FM;
    private FighterModule fighter_FD;
    private FighterModule fighter_BU;
    private FighterModule fighter_BM;
    private FighterModule fighter_BD;
    private WeaponModule weapon;
    private PokemonMoudle pokemonMoudle;

    // 队伍信息
    private String name;// 名字
    private int head;// 头像
    private int level;// 等级
    private long power;// 战力
    private String playerCode;//code

    private final static String POKEMON_NAME = "神兽";

    public String getPlayerCode() {
        return playerCode;
    }

    public void setPlayerCode(String playerCode) {
        this.playerCode = playerCode;
    }

    public FighterModule getFighter_FU() {
        return fighter_FU;
    }

    public void setFighter_FU(FighterModule fighter_FU) {
        this.fighter_FU = fighter_FU;
    }

    public FighterModule getFighter_FM() {
        return fighter_FM;
    }

    public void setFighter_FM(FighterModule fighter_FM) {
        this.fighter_FM = fighter_FM;
    }

    public FighterModule getFighter_FD() {
        return fighter_FD;
    }

    public void setFighter_FD(FighterModule fighter_FD) {
        this.fighter_FD = fighter_FD;
    }

    public FighterModule getFighter_BU() {
        return fighter_BU;
    }

    public void setFighter_BU(FighterModule fighter_BU) {
        this.fighter_BU = fighter_BU;
    }

    public FighterModule getFighter_BM() {
        return fighter_BM;
    }

    public void setFighter_BM(FighterModule fighter_BM) {
        this.fighter_BM = fighter_BM;
    }

    public FighterModule getFighter_BD() {
        return fighter_BD;
    }

    public void setFighter_BD(FighterModule fighter_BD) {
        this.fighter_BD = fighter_BD;
    }

    public WeaponModule getWeapon() {
        return weapon;
    }

    public void setWeapon(WeaponModule weapon) {
        this.weapon = weapon;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHead() {
        return head;
    }

    public void setHead(int head) {
        this.head = head;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public long getPower() {
        return power;
    }

    public void setPower(long power) {
        this.power = power;
    }

    // func===========================================================================

    /**
     * 初始化怒气
     */
    public void initAngers(int[] angers) {
        if (angers != null && angers.length > 0) {
            WeaponModule niuBee = getWeapon();
            if (niuBee != null) {
                niuBee.setAnger(angers[0]);
            }
            for (int i = 1; i < angers.length; i++) {
                FighterModule fighter = fighter(Position.forValue(i));
                if (fighter == null) {
                    continue;
                }
                fighter.setAnger(angers[i]);
            }
        }
    }

    public Map<Position, FighterModule> fighters() {
        HashMap<Position, FighterModule> map = new HashMap<>();
        for (Position position : Position.values()) {
            FighterModule fighter = fighter(position);
            if (fighter == null || fighter.hp() <= 0) {
                continue;
            }
            map.put(position, fighter);
        }
        return map;
    }

    public FighterModule fighter(Position position) {
        switch (position) {
            case FU:
                return getFighter_FU();
            case FM:
                return getFighter_FM();
            case FD:
                return getFighter_FD();
            case BU:
                return getFighter_BU();
            case BM:
                return getFighter_BM();
            case BD:
                return getFighter_BD();
        }
        return null;
    }

    public void addfighter(Position position, FighterModule fighterModule) {
        if (fighterModule == null) {
            return;
        }
        switch (position) {
            case FU:
                setFighter_FU(fighterModule);
                break;
            case FM:
                setFighter_FM(fighterModule);
                break;
            case FD:
                setFighter_FD(fighterModule);
                break;
            case BU:
                setFighter_BU(fighterModule);
                break;
            case BM:
                setFighter_BM(fighterModule);
                break;
            case BD:
                setFighter_BD(fighterModule);
                break;
        }
    }

    public Team team(boolean left) {
        Map<Position, FighterModule> fighters = fighters();
        Team team = new Team();
        addteam(team, fighters, Position.FU, left);
        addteam(team, fighters, Position.FM, left);
        addteam(team, fighters, Position.FD, left);
        addteam(team, fighters, Position.BU, left);
        addteam(team, fighters, Position.BM, left);
        addteam(team, fighters, Position.BD, left);
        addniubee(team, getWeapon(), left);
        //加入神兽
        addpokemon(team, getPokemonMoudle(), left);
        addCommanderSeal(this.getPlayerCode(), team);
        return team;
    }

    /**
     * 获取队伍平均武将属性
     */
    private FighterData getAvgFighterData(Team team) {
        //队伍上阵武将个数
        int generalsNums = team.getFighters().size();
        Map<PropertyType, Long> pokemonData = new HashMap<>();
        PropertyData propertyData = new PropertyData();
        PropertyType[] values = PropertyType.values();
        //获取队伍所有武将的属性集合
        List<Map<PropertyType, Long>> teamFighterDatas = team.getFighters().stream()
                .map(_fighter -> _fighter.getData().getPropertyData().getProperties())
                .collect(Collectors.toList());
        //计算武将平均属性
        for (PropertyType value : values) {
            long avg = 0;
            for (Map<PropertyType, Long> teamFighterData : teamFighterDatas) {
                avg += teamFighterData.get(value) == null ? 0L : teamFighterData.get(value);
            }
            //平均属性
            if(generalsNums == 0){
                avg = 0;
            }else{
                avg = Math.floorDiv(avg, generalsNums);
            }
            pokemonData.put(value, avg);
        }
        propertyData.setProperties(pokemonData);
        FighterData pokemonFighterData = new FighterData(propertyData, POKEMON_NAME);
        return pokemonFighterData;
    }

    //添加阵营相克功能
    private void addCommanderSeal(String playerCode, Team team) {
        if (StringUtils.isEmpty(playerCode)) return;
        PickedTroopsModule module = PickedTroopsModule.get(playerCode);

        HashMap<String, Double> commanderSeal = new HashMap<>();
        for (PickedTroops pickedTroops : module.getPts().values()) {
            commanderSeal.put(Camp.forNumber(pickedTroops.getCamp()) + "_" + 0, Double.parseDouble(String.valueOf(pickedTroops.getDamageincrease())));
            commanderSeal.put(Camp.forNumber(pickedTroops.getCamp()) + "_" + 1, Double.parseDouble(String.valueOf(pickedTroops.getDamagedrop())));
        }
        team.setCommanderSeal(commanderSeal);
    }

    private void addteam(Team team, Map<Position, FighterModule> fighters, Position position, boolean left) {
        FighterModule fighterModule = fighters.get(position);
        if (fighterModule == null) {
            return;
        }

        try {
//			long power = BattleFomula.战力(BattleHandle.packProperty属性增幅(PropertyModule.propertyData(fighterModule.getProperty())));

            PropertyData propertyData = PropertyModule.propertyData(fighterModule.getProperty());//战斗属性赋值
            //为了测试，先屏蔽掉
//			propertyData.addAllValue(packProperty羁绊(fighterModule, fighters.values())); // 羁绊
//			propertyData = BattleHandle.packProperty属性增幅(propertyData);

            long 初始血量 = propertyData.getValue(PropertyType.P_生命);
            long 实际血量 = 初始血量;
            if (fighterModule.getInitHp() != null) {
                实际血量 = fighterModule.getInitHp().handle(fighterModule, propertyData);
            }
            if (实际血量 <= 0) {
                // 这人死了
                return;
            }

            Fighter fighter = Fighter.createFighter(team, position, 实际血量, fighterModule.getHeroId(), fighterModule.getLevel(), fighterModule.getStage(),
                    fighterModule.getStarlv(), fighterModule.getAnger(), power, propertyData, fighterModule.getSkills());

            fighter.setFighterPosition(left);
            team.addFighterInPosition(fighter, position);
        } catch (Exception e) {
            System.err.println(fighterModule.getHeroId());
            e.printStackTrace();
        }
    }

    private void addniubee(Team team, WeaponModule weaponModule, boolean left) {
        if (weaponModule == null) {
            return;
        }
        weapondata data = DataCenter.getData(weaponModule.getWeaponId(), weapondata.class);
        if (data == null) {
            return;
        }

        try {
            NiuBee niubee = new NiuBee(team, data, weaponModule.getSkillLevel());
            niubee.setFighterPosition(left);
            niubee.initAnger(weaponModule.getAnger());
            team.set神兵(niubee);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int test_index1 = 0;
    private static int test_index2 = 0;

    /**
     * 初始化阵营的时候放入测试神兽
     */
    private void addpokemon(Team team, PokemonMoudle pokemonMoudle, boolean left) {
        if (pokemonMoudle == null) {
            return;
        }
        FighterData avgFighterData = getAvgFighterData(team);
        int skillGroupId = pokemonMoudle.getSkillGroupId();
        Pokemon pokemon1 = new Pokemon(team, skillGroupId, 1, avgFighterData);
        pokemon1.setFighterPosition(left);
        team.setPokemon(pokemon1);
    }

    public long power() {
        return Double.valueOf(team(false).getBattlePower()).longValue();
    }

    // 羁绊属性封装================================================================================
    public static PropertyData packProperty羁绊(FighterModule fighterModule, Collection<FighterModule> heros) {

        Map<Camp, Integer> 计数 = new HashMap<>();
        for (FighterModule heroo : heros) {
            int dataId = heroo.getHeroId();
            herodata data = DataCenter.getData(dataId, herodata.class);
            Camp camp = Camp.forNumber(data.getPower());
            int number = 0;
            if (计数.containsKey(camp)) {
                number = 计数.get(camp);
            }
            计数.put(camp, number + 1);
        }
        Set<fetterdata> fetterList = new HashSet<>();
        boolean allhaveone = true;
        for (Camp camp : Camp.values()) {
            if (!计数.containsKey(camp)) {
                allhaveone = false;
                continue;
            }
            int number = 计数.get(camp);
            if (number > 1) {
                allhaveone = false;
            }
            fetterdata data = findfetter(camp.getNumber(), number);
            if (data != null) {
                fetterList.add(data);
            }
        }
        if (allhaveone) {
            fetterdata data = findfetter(7, 6);// 各阵营都有一个
            fetterList.add(data);
        }
        herodata _herodata = DataCenter.getData(fighterModule.getHeroId(), herodata.class);
        PropertyData propertyData = new PropertyData();
        for (fetterdata data : fetterList) {
            if (data.getFettertype() != 7 && data.getFettertype() != _herodata.getPower()) {
                continue;
            }

            for (int i = 0; i < data.getType().length; i++) {
                int type = data.getType()[i];
                int value = data.getPercentage()[i];
                PropertyType propertyType = PropertyType.forNumber(type);
                if (propertyType == null || value == 0) {
                    continue;
                }
                propertyData.addValue(propertyType, value);
            }
        }
        return propertyData;
    }

    private static fetterdata findfetter(int camp, int number) {
        Collection<fetterdata> datas = DataCenter.getAllData(fetterdata.class).values();
        for (fetterdata data : datas) {
            if (data.getFettertype() == camp && data.getFetternum() == number) {
                return data;
            }
        }
        return null;
    }

    public PokemonMoudle getPokemonMoudle() {
        return pokemonMoudle;
    }

    public void setPokemonMoudle(PokemonMoudle pokemonMoudle) {
        this.pokemonMoudle = pokemonMoudle;
    }
}
